package cn.edu.zafu.bigdata.lekang.shiyan9;

import java.util.Scanner;
import java.sql.SQLException;

public class StudentManagementSystem {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    searchStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    deleteStudent();
                    break;
                case "5":
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选项，请重新输入。");
                    break;
            }
        }
    }

    private static void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        try {
            DatabaseManager.addStudent(new Student(id, name, phone));
            System.out.println("学生信息添加成功。");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            System.out.println("请重新输入学号。");
        }
    }

    private static void searchStudent() {
        while (true) {
            System.out.print("请输入学号或输入'QUIT'退出: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try {
                Student student = DatabaseManager.searchStudent(id);
                if (student != null) {
                    System.out.println(student);
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败。");
            }
        }
    }

    private static void updateStudent() {
        while (true) {
            System.out.print("请输入学号或输入'QUIT'退出: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try {
                Student student = DatabaseManager.searchStudent(id);
                if (student != null) {
                    System.out.print("请输入新的姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入新的电话: ");
                    String phone = scanner.nextLine();
                    student.setName(name);
                    student.setPhone(phone);
                    DatabaseManager.updateStudent(student);
                    System.out.println("学生信息更新成功。");
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败。");
            }
        }
    }

    private static void deleteStudent() {
        while (true) {
            System.out.print("请输入学号或输入'QUIT'退出: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try {
                DatabaseManager.deleteStudent(id);
                System.out.println("学生信息删除成功。");
            } catch (SQLException e) {
                System.out.println("删除失败，未找到该学生。");
            }
        }
    }
}